package cn.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库连接工具类
 * @author yale
 * @create 2017/10/9
 * @version 0.1
 * @see
 *
 */
public class DBConnection {
    //连接相关属性
    private static String url = "jdbc:mysql://localhost:3306/hundsent";
    private static String dbuname = "root";
    private static String dbpword = "admin";
    private static Connection con = null;

    public static Connection getCon(){
        if(null == con){
            //加载类
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection(url, dbuname, dbpword);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        return con;
    }

    /**
     * 关闭连接
     */
    public static void closeAll(){
        if (null != con) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
